//
//  HeaderTVCell.m
//  MMLOOK
//
//  Created by dllo on 15/9/16.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "HeaderTVCell.h"
#import "HeaderCVCell.h"

@interface HeaderTVCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UIScrollView *Scrol;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation HeaderTVCell

- (void)dealloc
{
    [_posterCollectionView release];
    [_flowLayout release];
    [_pageControl release];
    [_timer release];
    [_arr release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupCollectionView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width= self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    _flowLayout.itemSize = CGSizeMake(width, height);
    _posterCollectionView.frame = CGRectMake(0, 0, width, height);
    _pageControl.numberOfPages = _arr.count - 2;
    _posterCollectionView.contentOffset = CGPointMake(width, 0);
    _pageControl.frame = CGRectMake(width - (_arr.count - 2) * 17 - 10, height - 20, (_arr.count - 2) * 17, 15);
}

#warning ------------轮播图
- (void)sendArr:(NSMutableArray *)array
{
    self.arr = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        if ([[dic objectForKey:@"target_url"] isEqualToString:@""]) {
            [self.arr addObject:dic];
        }
    }
    if (self.arr.count != 0) {
        [self addImages];// 分别在数组最前面插入最后一张，最后面插入第一张
        [_posterCollectionView reloadData];
    }
}

- (void)setupCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.posterCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    _posterCollectionView.delegate = self;
    _posterCollectionView.dataSource = self;
    _posterCollectionView.showsHorizontalScrollIndicator = NO;//关闭显示的滚动条
    _posterCollectionView.pagingEnabled = YES;//按页数翻
    [_posterCollectionView registerClass:[HeaderCVCell class] forCellWithReuseIdentifier:@"kCellReuseIdentifier"];
    [self addSubview:_posterCollectionView];
    [_posterCollectionView release];
    [_flowLayout release];
    
    self.pageControl = [[UIPageControl alloc] init];
    _pageControl.backgroundColor = [UIColor colorWithHue:0.5 saturation:0.5 brightness:0.5 alpha:0.5];
    _pageControl.layer.cornerRadius = 7;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    _pageControl.defersCurrentPageDisplay = YES;
    [self addSubview:_pageControl];
    [_pageControl release];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

#pragma mark 轮播图跳页
- (void)timerAction:(NSTimer *)timer
{
    CGPoint newOffset = CGPointMake(_posterCollectionView.contentOffset.x + CGRectGetWidth(_posterCollectionView.frame), _posterCollectionView.contentOffset.y);
    [_posterCollectionView setContentOffset:newOffset animated:YES];
}

#pragma mark 插图方法
- (void)addImages
{
    id imageFirst = [_arr firstObject];
    id imageLast = [_arr lastObject];
    
    [_arr insertObject:imageLast atIndex:0];// 把之前的最后一张图加到最前面
    [_arr addObject:imageFirst];// 第一张图加到最后
}

#pragma mark 实现collectionView协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeaderCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCellReuseIdentifier" forIndexPath:indexPath];
    cell.image_url = [[_arr objectAtIndex:indexPath.row] objectForKey:@"image_url"];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jump:[self.arr objectAtIndex:indexPath.item]];
}

#pragma mark 实现scrollView协议、循环滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 循环滚动
    if (scrollView.contentOffset.x >= scrollView.frame.size.width * ([_arr count] - 1)) {// 跳到第一个
        
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
    } else if (scrollView.contentOffset.x <= 0) {// 跳到最后一个
        
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * ([_arr count] - 2), 0) animated:NO];
    }
    
    _pageControl.currentPage = scrollView.contentOffset.x  / scrollView.frame.size.width - 1;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
